package pl.edu.agh.admin.gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

import pl.edu.agh.admin.api.CronTabManager;
import pl.edu.agh.admin.api.CronTabManagerProvider;
import pl.edu.agh.admin.api.PermissionDeniedException;
import pl.edu.agh.admin.api.Task;
import pl.edu.agh.admin.api.User;
import pl.edu.agh.admin.core.UserImpl;

public class EditTaskDialog extends JDialog implements ActionListener
{
	private static final long serialVersionUID = 2814;
	private JButton okButton;
	private JButton cancelButton;
	private JTextField userTextField;
	private JTextField commandTextField;
	private JTextField scheduleTextField;
	private JTable taskTable;
	private TaskTableModel taskTableModel;
	
	
	
	public EditTaskDialog(JTable taskTable)
	{
		this.taskTable = taskTable;
		this.taskTableModel = (TaskTableModel)taskTable.getModel();
		
		setTitle("Final Pyton 88");
		setSize(350, 150);
        setLocationRelativeTo(null);
        
        Task task = getSelectedTask();
		String command = task.getCommand();
		User user = task.getUser();
		String schedule = task.getCron();
        
        
        setLayout(new FlowLayout(FlowLayout.CENTER));

        
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 2));
        
        panel.add(new JLabel("Uzytkownik: ", JLabel.RIGHT));
        panel.add(userTextField = new JTextField(user.getName(), 14));
        
        panel.add(new JLabel("Kiedy: ", JLabel.RIGHT));
        panel.add(scheduleTextField = new JTextField(schedule, 14));
        
        panel. add(new JLabel("Komenda: ", JLabel.RIGHT));
        panel.add(commandTextField = new JTextField(command, 14));
        
        add(panel);
        
        okButton = new JButton("ok");
        okButton.addActionListener(this);
        add(okButton);
        
        cancelButton = new JButton("anuluj");
        cancelButton.addActionListener(this);
        add(cancelButton);
	}



	public void actionPerformed(ActionEvent event) 
	{
		if(event.getSource() == okButton)
		{
			CronTabManager cronTabMgr = CronTabManagerProvider.getCronTabManager();
			try
			{
				cronTabMgr.deleteTask(getSelectedTask());
			}
			catch (PermissionDeniedException e) 
			{
				JOptionPane.showMessageDialog(this, "Brak dostepu", "Nie moza usunac zadania. Brak Uprawnien", JOptionPane.ERROR_MESSAGE);
				dispose();
				return;
			}
			
			String command = commandTextField.getText();
			User user = new UserImpl(userTextField.getText());
			String schedule = scheduleTextField.getText();

			try 
			{
				Task task = cronTabMgr.createTask(user, schedule, command);
			} catch (PermissionDeniedException e) 
			{
				JOptionPane.showMessageDialog(this, "Brak uprawnien", "Nie moza utworzyc zadania. " + e.getMessage(), JOptionPane.ERROR_MESSAGE);
			}
			
			taskTableModel.refresh();
			taskTableModel.fireTableDataChanged();
			dispose();
		}
		else if(event.getSource() == cancelButton)
		{
			dispose();
		}
	}
	
	
	
	private Task getSelectedTask()
	{
		int selectedTask = taskTable.getSelectedRow();
		Task task = taskTableModel.getTask(selectedTask);

		return task;
	}
}
